<template>
	<view class="content">

		<view class="top">
			<view class="find">
				<image src="../../static/sousuo.png" mode="widthFix"></image>
			</view>
			<input placeholder="请输入搜索关键字..." class="input" v-model="input" />
		</view>

		<view class="nav">
			<view class="navbox">
				<view tabindex="1" @click="getAnyData">默认</view>
		 	<view tabindex="2" @click="getBuyData">销量</view>
				<view tabindex="3" @click="getNewgoodData">新品</view>
				<view tabindex="4" @click="getPriceData">价格<view class="pricess"> <view >
					<image class="on" src="../../static/升序.png" mode="widthFix" @click="ons"></image> 
					<view > </view> <image  class="up" src="../../static/降序.png" mode="widthFix" @click="unders"></image> 
					 </view>  </view></view>
			</view>
		</view>

		<view class="showbox">
			<view v-for="item in sweetList" :key="ttem.id" class="showitem">
				<view>
					<image :src="$host+item.src" mode="widthFix"></image>
				</view>
			 <view class="title">{{item.title}}</view>
				<view class="bottom">
					<view class="money">￥{{item.money}}</view>
					
					<view class="jian" @click="subtract" :data-index="{id:item.id,count:item.count}" v-show="item.addState">-</view>
					<text class="number" v-show="item.addState">{{item.count}}</text><view  class="add" @click="add" :data-index="{id:item.id,count:item.count,addState:item.addState}">＋</view>
				</view>
			</view>
		</view>
        
		<view class="cars" @click="goshop">
			<image src="../../static/购物车.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {

			return {
				$host: this.$host,
				sweetList: [],
				input:"",
			     btn:true,
				 goodList:[]   
				
			}
		},
		onLoad() {
			this.$request("/getClassData", "post", {}).then(res => {
				this.sweetList = res.data
			})
		},
		onShow(){
				this.getAnyData()
		},
		methods: {
			unders(){
				this.$request("/unders","post",{}).then(res=>{
									    this.sweetList=res.data.filter(item=>item.navTitle==this.class)
				})
			},
			ons(){
				 this.$request("/on","post",{}).then(res=>{
					    this.sweetList=res.data.filter(item=>item.navTitle==this.class)
				 })
			},
			getAnyData() {
				this.$request("/getClassData", "post", {}).then(res => {
					this.sweetList = []
					this.sweetList = res.data
				
				})
			},
			getBuyData() {
				this.$request("/getBuyData", "post", {
					host: "host"
				}).then(res => {
					this.sweetList = []
					this.sweetList = res.data
					
				})
			},
			getNewgoodData() {
				this.$request("/getNewData", "post", {
					new: "new"
				}).then(res => {
					this.sweetList = []
					this.sweetList = res.data
					
				})
			},
			getPriceData() {

			},
			// 添加购物车,注每增加一个物品就需要向goodList中push进去该物品的基本信息,为后面购物车页面做铺垫
			add(e){
				
			
				   
				   if(e.currentTarget.dataset.index.count==null || e.currentTarget.dataset.index.addState==null){
					    e.currentTarget.dataset.index.count=0
						e.currentTarget.dataset.index.addState=true
				
					   this.$request("/add","post",{id:e.currentTarget.dataset.index.id,count:parseInt
					   (e.currentTarget.dataset.index.count)+1, addState:e.currentTarget.dataset.index.addState})
					   .then(res=>{
					   	  this.getAnyData()		
						this.goodList.push(res.data[0])
													 let arr=this.goodList
						for (var i = 0; i < arr.length; i++) {    // 首次遍历数组
						     for (var j = i + 1; j < arr.length; j++) {   // 再次遍历数组
						         if (arr[i].id == arr[j].id && arr[i].count<arr[j].count) {          // 判断连个值是否相等
									   arr[i].count=arr[j].count
						             arr.splice(i, 1);           // 相等删除后者
						             j--;
						         }
						     }
						 }
						  
						  
						  this.goodList=arr
						
							this.$store.dispatch("addShopData",this.goodList)	
					   	 
					   })
					   
				   }else{
					   
					   
					     this.$request("/add","post",{id:e.currentTarget.dataset.index.id,count:parseInt(e.currentTarget.dataset.index.count)+1
						 ,addState:true}).then(res=>{													 
					     	  this.getAnyData()		
							this.goodList.push(res.data[0])
						
							 let arr=this.goodList
						   for (var i = 0; i < arr.length; i++) {    // 首次遍历数组
						        for (var j = i + 1; j < arr.length; j++) {   // 再次遍历数组
						            if (arr[i].id == arr[j].id && arr[i].count<arr[j].count) {          // 判断连个值是否相等
									   arr[i].count=arr[j].count
						                arr.splice(j, 1);           // 相等删除后者					              
										j--;
						            }
						        }
						    }
				             this.goodList=arr
						  this.$store.dispatch("addShopData",this.goodList)	
							
							
												
								 
					     	 
					     })
						  
				   }
				 
			
				   
			
			},
			subtract(e){
		// 注没减少一个物品就需要向goodList中删除该物品的基本信息,为后面购物车页面做铺垫
		   if(e.currentTarget.dataset.index.count>1){
			   this.$request("/subtract","post",{id:e.currentTarget.dataset.index.id,count:parseInt(e.currentTarget.dataset.index.count)-1,addState:true}).then(res=>{
			   	  this.getAnyData()	
				 this.goodList=this.goodList.filter(item=>item.id!=e.currentTarget.dataset.index.id)
				 this.$store.dispatch("addShopData",this.goodList)	
				   
					
				  
			   	 
			   })	   
		   }else if(e.currentTarget.dataset.index.count<=1){
			            
			        this.$request("/add","post",{id:e.currentTarget.dataset.index.id,count:parseInt
			        (e.currentTarget.dataset.index.count)-1, addState:false}).then(res=>{
			        	  this.getAnyData()		
			        					
			        	 this.goodList=this.goodList.filter(item=>item.id!=e.currentTarget.dataset.index.id)
						 this.$store.dispatch("addShopData",this.goodList)	
			        })
					
					 
		   }
		   
			
			},
			// 点击购物车	
			goshop(){
				
			  if(this.sweetList.filter(item=>item.addState==true).length<=0 ){
				  uni.navigateTo({
				  	url:"/pages/shopCars/noShop"
				  })
			  }else{
				   uni.navigateTo({
				   	url:"/pages/shopCars/index"
				   })
			  }
			  
			
			}
			
			
			
			
			
		},
		watch:{
			input:function(){
				this.$request("/getClassData", "post", {
					new: "new"
				}).then(res => {
					this.sweetList = []
					this.sweetList=res.data.filter(item=>item.title.indexOf(this.input)!=-1)														
				})
			}
		}
	}
</script>

<style>
	.pricess image{
		   width: 46rpx;
	}
	.pricess{
		position: relative;
		top: -75rpx;
		left: 56rpx;
	}
	.on{
		position: relative;
		top: 26rpx;
	}
	.up{
		position: relative;
		top: 7rpx;
	}
	#active{
		display: none;
	}
	/* 搜索框 */
	.top {
		height: 100rpx;
		width: 100%;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		/* border: 1px solid grey; */
		flex-basis: 84%;
		height: 70rpx;
		background-color: #f6f6f6;
		border-radius: 30rpx;
		padding-left: 82rpx;


	}

	.find>image {
		width: 50rpx;
	}

	.find {
		width: 50rpx;
		position: absolute;
		top: 26rpx;
		left: 42rpx;
	}

	.navbox {
		display: flex;
		justify-content: space-around;
		color: #757575;
	}

	.nav {
		width: 300px;
		margin-top: 10rpx;
	}

	.navbox>view:focus {
		font-weight: 600;
		color: black;
	}

	.showbox image {
		width: 170px;

	}

	.showbox {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
		margin-top: 28rpx;
	}

	.showitem {
		display: flex;
		flex-direction: column;
		border: 1px solid #f6f6f6;
		width: 170px;
		margin-top: 20rpx;
	}

	.bottom {
		display: flex;
		justify-content: space-between;
		padding-bottom: 10rpx;
	}

	.title {
		font-weight: 600;
		font-size: 32rpx;
		margin-left: 8rpx;
	}

	.money {
		color: #fe9a69;
		font-size: 30rpx;
		margin-top: 10rpx;
	}

	.add {
		width: 39rpx;
		height: 39rpx;
		color: white;
		font-size: 36rpx;
		font-weight: 600;
		background-color: #fe9a69;
		border-radius: 50%;
		margin-top: 10rpx;
		line-height: 39rpx;
		text-align: center;
		position: relative;
		left: -17rpx;
	}
	.jian{
		position: relative;
		left: 22rpx;
		width: 39rpx;
		height: 39rpx;
		color: white;
		font-size: 36rpx;
		font-weight: 600;
		background-color: #fe9a69;
		border-radius: 50%;
		margin-top: 10rpx;
		line-height: 39rpx;
		text-align: center;
	}
	.number{
		margin-top: 8rpx;
	}
	.cars{
		text-align: center;
	
		width: 60px;
		height: 60px;
		border-radius: 50%;
	    background-color: white;
		box-shadow: 1rpx 1rpx lightslategrey;
		position: fixed;
		right: 1px;
		top: 360px;
		z-index: 50;
		
	}
	.cars>image{
		width: 30px;
		position: relative;
		top: 14px;
	}
</style>
